/**
 * @Description:
 * @Author: CHEHSHUANG
 * @Date: 2019/3/13
 */
import React, {Component} from 'react';
import {Form, Input, Radio, Modal, Checkbox} from "antd";

const FormItem = Form.Item;
const RadioGroup = Radio.Group;

class ItemAddOrEditModal extends Component {
  state = {
    featureType: "Operate"
  }
  componentDidMount() {
    this.props.onRef(this);
  }
  checkFeatureType = (rule, value, callback) => {
    if (value === "Operate") {
      this.props.form.setFieldsValue({
        'biddingCondition.confirmTypeEnum': 'ConfirmByRate',
        'biddingCondition.canChooseQuoted': false
      });
    }
    callback();
  };
  render() {
    const formItemLayout = {
      labelCol: {
        span: 8
      },
      wrapperCol: {
        span: 12
      },
    };

    const {parentData,confirmLoading, addVisible, handleOk, handleCancel, defaultValue, flag} = this.props;
    const {getFieldDecorator} = this.props.form;
    const {featureType} = this.state;
    let formValue = defaultValue;
    let title = "编辑功能项";
    if (flag === "refAdd") {
      title = "参考创建";
    } else if (flag === "add") {
      title = "新增功能项";
      formValue = {}
    }
    return (
      <Modal
        title={title}
        visible={addVisible}
        onOk={handleOk}
        onCancel={handleCancel}
        width={600}
        afterClose={this.handleClose}
        confirmLoading={confirmLoading}
        maskClosable={false}
      >
        <Form>
          <FormItem
            {...formItemLayout}
            label="功能项类型"
          >
            {getFieldDecorator('featureType', {
              initialValue: featureType === "Business" ? "Business" : "Operate",
              rules: [{required: true, message: '请选择功能项类型'}, {validator: this.checkFeatureType}],
            })(
              <RadioGroup className={'radio-group'}>
                <Radio value={"Operate"}>操作</Radio>
                <Radio value={"Business"}>业务</Radio>
              </RadioGroup>
            )}
          </FormItem>
          <FormItem
            {...formItemLayout}
            label="代码"
          >
            {getFieldDecorator('code', {
              initialValue: formValue.code ? formValue.code : "",
              rules: [{required: true, message: '请填写代码!',whitespace:true}]
            })(
              <Input/>
            )}
          </FormItem>
          <FormItem
            {...formItemLayout}
            label="名称">
            {getFieldDecorator('name', {
              initialValue: formValue.name ? formValue.name : "",
              rules: [{required: true, message: '请填写名称!'}]
            })(
              <Input/>
            )}
          </FormItem>
          <FormItem
            {...formItemLayout}
            label="页面资源">
            {getFieldDecorator('url', {
              initialValue: parentData.url ? parentData.url : "",
            })(
              <Input disabled={true}/>
            )}
          </FormItem>
          <FormItem
            style={{display: "none"}}
            label="是否为菜单项">
            {getFieldDecorator('canMenu', {
              initialValue:  false,
            })(
              <Input disabled/>
            )}
          </FormItem>
          <FormItem
            {...formItemLayout}
            label="租户是否可用">
            {getFieldDecorator('tenantCanUse', {
              valuePropName: 'checked',
              initialValue: formValue.tenantCanUse ? formValue.tenantCanUse : false,
            })(
              <Checkbox/>
            )}
          </FormItem>
          {/*以下为隐藏的formItem*/}
          <FormItem
            style={{display: "none"}}>
            {getFieldDecorator('id', {
              initialValue: formValue.id ? formValue.id : "",
            })(
              <Input disabled/>
            )}
          </FormItem>
        </Form>
      </Modal>
    );
  }
}

export default Form.create()(ItemAddOrEditModal);
